/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.switz.agendacita.persistence;

import com.switz.agendacita.dto.PersonalServicio;
import com.switz.agendacita.dto.Servicio;
import com.switz.agendacita.persistence.iface.PersonalServicioDao;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author jose angel
 */
@Repository
public class PersonalServicioDaoImpl extends JdbcDaoSupport implements PersonalServicioDao {

    @Autowired
    public PersonalServicioDaoImpl(DataSource dataSource) {

        setDataSource(dataSource);
    }

    @Override
    public void insertBatchPersonalServicio(List personalServicioList) {
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(
                getJdbcTemplate().getDataSource())
                .withTableName("personalservicios");
        SqlParameterSource[] array = SqlParameterSourceUtils.createBatch(personalServicioList.toArray());



        simpleJdbcInsert.executeBatch(array);
    }

    @Override
    public void deleteServiciosPorPersonal(String idPersonal, String idServicio) {
        String query = "delete from personalservicios where idpersonal = ? and idservicio=?";
        getJdbcTemplate().update(query, new Object[]{idPersonal,idServicio});
    }

	@Override
	public void deletePersonalServicio(String idEmpresa, String idServicio) {
		String query = "delete from personalservicios where idempresa = ? and idservicio = ?";
        getJdbcTemplate().update(query, new Object[]{idEmpresa,idServicio});
		
	}

    @Override
    public List getServicios(String idEmpresa,String idPersonal) {
    	List servicios = getJdbcTemplate().query("SELECT descripcion from servicios inner join personalservicios"
        				+" on personalservicios.idservicio=servicios.id "
        				+"where personalservicios.idempresa=? and personalservicios.idpersonal =?", new Object[] {idEmpresa,idPersonal},
        ParameterizedBeanPropertyRowMapper.newInstance(Servicio.class));
        		
        return servicios;
    }

}
